package com.aqie.arithmetic.datastructure;

import java.util.Iterator;
import java.util.Stack;

/**
 * 链表实现栈
 */
public class LinkedListStack<T> implements Iterable<T>{
    private Node first;
    private int N;
    private class Node{
        T item;
        Node next;
    }

    public boolean isEmpty(){
        return first == null;
    }

    public int size(){
        return N;
    }

    public void push(T item){
        // 向栈顶添加元素
        Node oldfirst = first;
        first = new Node();
        first.item = item;
        first.next = oldfirst;
        N++;
    }

    public T pop(){
        // 从栈顶删除元素
        T item = first.item;
        first = first.next;
        N--;
        return item;
    }

    @Override
    public Iterator<T> iterator() {
        return new ListIterator();
    }

    private class ListIterator implements Iterator<T>{
        private Node current = first;
        public boolean hasNext(){
            return current != null;
        }
        public void remove(){}

        public T next(){
            T item = current.item;
            current = current.next;
            return item;
        }
    }

    public static void main(String[] args) {
        // 字符串栈的数组
        Stack<String>[] a = (Stack<String>[]) new Stack[10];
    }
}
